package com.czd.support.controller;

import java.util.List;
import com.czd.support.common.Constants;
import com.czd.support.model.Advertisement;
import com.czd.support.model.DataDictionary;
import com.czd.support.model.Manager;
import com.czd.support.service.AdvertisementService;
import com.czd.support.service.DataDictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Author: zhangrui
 * Create: zhangrui (2014/11/25)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/advertisement")
public class AdvertisementController extends BaseController {

    @Autowired
    private AdvertisementService advertisementService;
    
    @Autowired
    private DataDictionaryService dataDictionaryService;
    
    /**
     * 广告分类字典
     * @return
     */
    @ModelAttribute("adCategoryTypeList")
    protected List<DataDictionary> initAdCategoryType(){
        return dataDictionaryService.queryListByCode(Constants.ADVERTISEMENT_CODE);
    }

    /**
     * 广告列表页
     * @param model
     * @param advertisement
     * @return
     */
    @RequestMapping(value = {"","/","list"},method = RequestMethod.GET)
    public String list(Model model,Advertisement advertisement,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
    	advertisement.setPageNo(pageNo);
        model.addAttribute("paginate",advertisementService.queryForPaginate(advertisement));
        return "advertisement/list";
    }

    /**
     * 新增广告拦截
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "advertisement/add";
    }
    
    /**
     * 新增广告
     * @param advertisement
     * @param request
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Advertisement advertisement,HttpServletRequest request,Model model){
        Manager manager = this.getSesionUser(request);
        advertisement.setCreateTime(manager.getCreateTime());
        advertisement.setCreateUser(manager.getCreateUser());
        if(advertisementService.insert(advertisement)){
        	return "redirect:/advertisement/list";
        }
        model.addAttribute("msg","创建失败");
        return "advertisement/add";
    }
    
    /**
     * 广告编辑 拦截
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/edit",method = RequestMethod.GET)
    public String view(@PathVariable int id,Model model){
    	Advertisement advertisement = advertisementService.findById(id);
        if(advertisement != null){
            model.addAttribute("advertisement",advertisement);
            return "advertisement/edit";
        }
        return error(model,"操作错误","广告不存在");
    }
    
    /**
     * 广告编辑
     * @param id
     * @param advertisement
     * @param request
     * @return
     */
    @RequestMapping(value = "/{id}/update",method = RequestMethod.POST)
    public String update(@PathVariable int id,Advertisement advertisement,HttpServletRequest request,Model model){
    	Advertisement advertisementData = advertisementService.findById(id);
        if(advertisementData != null){
            Manager manager = this.getSesionUser(request);
            advertisement.setUpdateTime(manager.getUpdateTime());
            advertisement.setUpdateUser(manager.getUpdateUser());
            if(StringUtils.isBlank(advertisement.getTitle())){
                model.addAttribute("error","广告名称不允许为空");
                return "advertisement/edit";
            } else {
                if (advertisementService.update(advertisement)) {
                        return "redirect:/advertisement/list";
                    } else {
                        return error(model, "操作失败", "更新广告信息失败");
                }
            }
        }else{
            return error(model, "操作失败", "此广告已经不存在");
        }
    }
    
    /**
     * 广告删除 
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/del",method = RequestMethod.POST)
    public String del(@PathVariable int id,Model model){ 
        if(advertisementService.deleteById(id)){
        	return "redirect:/advertisement/list";
        } else {
        	return error(model,"操作错误","广告删除失败");
        }
    }
}

